package com.study.springboot.showinfo.contoller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author huyong
 * @Decription
 * @date 2025-03-27 17:43
 */
@RestController
@RequestMapping("/info")
public class ShowInfoContoller {


    @RequestMapping(value = "/show", produces = "application/json")
    @ResponseBody
    public Map<String, List<String>> showInfo(@RequestParam("header") String header, HttpServletRequest request) {
        Enumeration<String> headers = request.getHeaders(header);
        Map<String, List<String>> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        map.put(header, list);
        if (headers == null) {
            return map;
        }
        while (headers.hasMoreElements()) {
            list.add(headers.nextElement());
        }
        return map;
    }

    @RequestMapping(value = "/headers", produces = "application/json")
    @ResponseBody
    public Map<String, List<String>> headers(HttpServletRequest request) {
        Enumeration<String> headers = request.getHeaderNames();
        Map<String, List<String>> map = new HashMap<>();
        if (headers == null) {
            return map;
        }
        while (headers.hasMoreElements()) {
            String headerName = headers.nextElement();
            Enumeration<String> headerList = request.getHeaders(headerName);
            List<String> list = new ArrayList<>();
            map.put(headerName, list);
            while (headerList.hasMoreElements()) {
                list.add(headerList.nextElement());
            }
        }
        return map;
    }

    @RequestMapping(value = "/param", produces = "application/json")
    @ResponseBody
    public Map<String, List<String>> param(HttpServletRequest request) {
        Enumeration<String> params = request.getParameterNames();
        Map<String, List<String>> map = new HashMap<>();
        if (params == null) {
            return map;
        }
        while (params.hasMoreElements()) {
            String paramName = params.nextElement();
            List<String> list = new ArrayList<>();
            map.put(paramName, list);
            list.add(request.getParameter(paramName));
        }
        return map;
    }
}
